<?php


namespace app\admin\controller\cms;

use app\admin\controller\Base;
use think\facade\Request;


class Block extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\cms\Block;
        $this->validate = new \app\admin\validate\cms\Block;
        $this->redict = url('index');
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if (Request::isPost()) {
            $title = $this->request->post('title', '', 'trim');

            $option = [
                'where' => [
                    ['title','like',"%".$title."%"]
                ]
            ];
            $list = $this->model->loadList($option);
            if ($list['result'] == 'success'){
                $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['list'], 'count' => $list['page']['total']];
            }else{
                $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => [], 'count' => 0];
            }
            return $result;
        }
        return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        if (Request::isPost()) {
            return parent::create();
        }else{
            return parent::add();
        }
        
    }
    // 状态修改
    public function state()
    {
        if (Request::isPost()) {
            $data = $this->request->post();
            $id = $this->request->post('id');
            if (empty($id)) {
                $this->error('id'.lang('not exist'));
            }
            $info = $this->model->find($id);
            $field = $data['field'];
            $status = $info->$field=='normal'?'hidden':'normal';
            $this->model->where('id',$id)->update(['status'=>$status]);
            $this->success(lang('edit success'));
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function edit(Request $request, $id)
    {
        if (Request::isPost()) {
            return parent::modify();
        }else{
            return parent::read('add');
        }
    }
}
